<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">
        当前位置：首页
      </el-breadcrumb-item>
      <el-breadcrumb-item>服务卡管理</el-breadcrumb-item>
      <el-breadcrumb-item>服务卡配置</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索区 -->
      <el-form
        ref="searchFormRef"
        :inline="true"
        :model="queryInfo.query"
        size="mini"
      >
        <el-form-item label="所属分类" prop="product_type">
          <el-select
            v-model="queryInfo.query.product_type"
            placeholder="请选择"
            filterable
            clearable
            @change="changeProductType"
          >
            <el-option
              v-for="item in selectData.product_type"
              :key="item.id"
              :label="item.name + '【' + item.id + '】'"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属产品" prop="product_id">
          <el-select
            v-model="queryInfo.query.product_id"
            placeholder="请选择"
            filterable
            clearable
          >
            <el-option
              v-for="item in selectData.product"
              :key="item.id"
              :label="item.name + '【' + item.id + '】'"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="服务卡类型" prop="card_type">
          <el-select
            v-model="queryInfo.query.card_type"
            placeholder="请选择"
            filterable
            clearable
          >
            <el-option key="0" label="单次卡" value="0"> </el-option>
            <el-option key="1" label="套卡" value="1"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关键字" prop="keyword">
          <el-input
            v-model="queryInfo.query.keyword"
            placeholder="请输入服务卡名称或ID"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            class="theme-background-color"
            size="mini"
            @click="searchList"
          >
            搜索
          </el-button>
          <el-button size="mini" @click="resetSearchForm"> 重置 </el-button>
        </el-form-item>
      </el-form>
      <!-- 操作区域 -->
      <div>
        <el-button
          class="theme-background-color"
          icon="el-icon-plus"
          size="mini"
          @click="$router.push('/servicecard/ServicecardAdd')"
        >
          新增服务卡
        </el-button>
        <el-button
          icon="el-icon-download"
          size="mini"
          :loading="btnLoading"
          @click="downloadExcel"
        >
          导出数据
        </el-button>
      </div>
      <!-- 用户列表区域 -->
      <el-table :data="list" border stripe>
        <!-- <el-table-column label="序号" type="index"></el-table-column> -->
        <el-table-column
          label="卡名称【ID】"
          min-width="150px"
          max-width="300px"
        >
          <template slot-scope="scope">
            {{ scope.row.name }}【{{ scope.row.id }}】
          </template>
        </el-table-column>
        <el-table-column label="所属分类" min-width="150px" max-width="300px">
          <template slot-scope="scope">
            {{ scope.row.product_type }}
          </template>
        </el-table-column>
        <el-table-column label="所属产品" min-width="150px" max-width="300px">
          <template slot-scope="scope">
            {{ scope.row.product_name }}【{{ scope.row.product_id }}】
          </template>
        </el-table-column>
        <el-table-column label="卡类型" prop="card_kind"></el-table-column>
        <el-table-column label="服务次数|时限" min-width="100px">
          <template slot-scope="scope">
            <span v-if="scope.row.card_kind == '次数卡'">{{
              scope.row.service_num
            }}</span>
            <span v-else>
              {{ scope.row.time_long }}
              <span v-if="scope.row.time_unit == 1">分钟</span>
              <span v-else-if="scope.row.time_unit == 2">小时</span>
              <span v-else-if="scope.row.time_unit == 3">日</span>
              <span v-else-if="scope.row.time_unit == 4">月</span>
              <span v-else-if="scope.row.time_unit == 5">年</span>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="有效期限">
          <template slot-scope="scope">
            {{ scope.row.period
            }}{{ scope.row.unit === '月' ? '个月' : scope.row.unit }}
          </template>
        </el-table-column>
        <el-table-column label="使用方式" prop="use_way_name"></el-table-column>
        <el-table-column
          label="适用范围"
          prop="apply_area"
          min-width="180px"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          label="使用区域"
          prop="area_limit_name"
        ></el-table-column>
        <el-table-column
          label="服务标准"
          prop="service_standard"
          min-width="220px"
          max-width="500px"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column label="状态" prop="status.name"></el-table-column>
        <el-table-column
          label="创建时间"
          prop="create_time"
          min-width="90px"
        ></el-table-column>
        <el-table-column label="操作" width="100px" fixed="right">
          <template slot-scope="scope">
            <el-link
              type="danger"
              v-if="scope.row.status.id == 1"
              @click="close(scope.row.id)"
            >
              关闭
            </el-link>
            <el-link
              type="primary"
              v-if="scope.row.status.id == 0"
              @click="open(scope.row.id)"
            >
              开启
            </el-link>
            &nbsp;
            <el-link
              type="primary"
              @click="
                $router.push({
                  path: '/servicecard/ServicecardEdit',
                  query: { id: scope.row.id }
                })
              "
            >
              编辑
            </el-link>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: {
          product_type: '',
          product_id: '',
          card_type: '',
          keyword: ''
        },
        pagenum: 1,
        pagesize: 20
      },
      selectData: [],
      list: [],
      total: 0,
      btnLoading: false
    }
  },
  created() {
    this.getSelectData()
    this.getListData()
  },
  methods: {
    async getSelectData() {
      const { data: res } = await this.$http.post('/admin/data/find', {
        code: 'product_type,product',
        level: 2
      })
      // 返回结果
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.selectData = res.data
    },
    async changeProductType(val) {
      const { data: res } = await this.$http.post('/admin/data/find', {
        code: 'product',
        type_code: val
      })
      // 返回结果
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.selectData.product = res.data.product
    },
    searchList() {
      this.queryInfo.pagenum = 1
      this.getListData()
    },
    resetSearchForm() {
      //   console.log(this)
      this.$refs.searchFormRef.resetFields()
      this.getListData()
    },
    async getListData() {
      // 返回数据重命名为res
      const { data: res } = await this.$http.post(
        '/admin/service_card_config/list',
        this.queryInfo
      )
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.list = res.data.list
      this.total = res.data.total
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      //   console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getListData()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      //   console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getListData()
    },
    async close(id) {
      // 弹框询问是否关闭
      const confirmResult = await this.$confirm(
        '确定关闭该服务卡配置？',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)

      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消操作')
      }
      const { data: res } = await this.$http.post(
        '/admin/service_card_config/editStatus',
        {
          id: id,
          status: 0
        }
      )
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
      }
      this.$message.success(res.meta.msg)
      this.getListData()
    },
    async open(id) {
      // 弹框询问是否开启
      const confirmResult = await this.$confirm(
        '确定开启该服务卡配置？',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)

      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消操作')
      }
      const { data: res } = await this.$http.post(
        '/admin/service_card_config/editStatus',
        {
          id: id,
          status: 1
        }
      )
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
      }
      this.$message.success(res.meta.msg)
      this.getListData()
    },
    async downloadExcel() {
      this.btnLoading = true
      this.$http({
        method: 'post',
        url: '/admin/service_card_config/export',
        data: this.queryInfo.query,
        headers: {
          token: window.sessionStorage.getItem('token')
        },
        responseType: 'blob'
      })
        .then((res) => {
          this.btnLoading = false
          if (res.data.type) {
            // 文件下载
            const blob = new Blob([res.data], {
              type: 'application/vnd.ms-excel'
            })
            let link = document.createElement('a')
            link.href = URL.createObjectURL(blob)
            link.setAttribute('download', '服务卡配置数据.xlsx')
            link.click()
            link = null
            this.$message.success('导出成功')
          } else {
            // 返回json
            this.$message.warning(res.data.msg)
          }
        })
        .catch((err) => {
          this.btnLoading = false
          this.$message.error(err)
        })
    }
  }
}
</script>

<style lang="less" scoped>
</style>
